home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / pstricks / contrib / piechart.sh < prev   
Linux/UNIX/POSIX Shell Script  |  1993-04-07  |  16KB  |  365 lines

  1. #! /bin/sh
  2.  
  3. #                               -*- Mode: Ksh -*-
  4. # piecharts.sh --- Creation of pie charts in (La)TeX + PostScript,
  5. #                  with PSTricks
  6. # Author          : Denis GIROU (CNRS/CIRCE - France) <girou@circe.fr>
  7. # Created the     : Fri Oct 16 21:53:41 1992
  8. # Last mod. by    : Denis GIROU (CNRS/CIRCE - France) <girou@circe.fr>
  9. # Last mod. the   : Tue Jan 26 21:15:55 1993
  10. # Version         : 2.2
  11. #
  12. # Description     :   piecharts.sh is a simple SHELL and AWK program to
  13. #                   realize hight quality, greatly customizable, grayscaled
  14. #                   or colored pie charts in PostScript.
  15. #                     It uses (La)TeX and the splendid PSTricks package of
  16. #                   Timothy Van Zandt <tvz@princeton.edu>, available in
  17. #                   /anonymous@princeton.edu:/pub
  18. #                     It creates the (La)TeX (+ PSTricks) code for direct
  19. #                   insertion in a (La)TeX document of the pie chart generated.
  20. #                   It use a data file which describe the parts and the labels
  21. #                   for them.
  22. #                     You always can modify the generated code for more
  23. #                   personal and sophisticated result.
  24. #
  25. # Syntax          : piecharts.sh [scale_factor] ([PLAIN] | [FIGURE] [BOXIT]) \
  26. #                                <data_file >output_file
  27. #                     Default scale factor is 1.
  28. #                     Default mode is LaTeX. In this case, use the FIGURE
  29. #                   option if you want to define the figure LaTeX environment
  30. #                   for the pie chart, and use the BOXIT option if you want
  31. #                   a box around this figure.
  32. #                     Use the PLAIN option if you want to generate a plain TeX
  33. #                   code (in this case, you cannot used the FIGURE and BOXIT
  34. #                   options).
  35. #
  36. # Input file      :   The fill styles are: none, solid, vlines, vlines*,
  37. #                   hlines, hlines*, crosshatch, crosshatch*
  38. #                     The predefined colors are : black, darkgray, gray,
  39. #                   lightgray, white, red, green, blue, cyan, magenta, yellow
  40. #                     You can easily define new gray or colors, or access to a
  41. #                   rgb palette with the palette.sty file of PSTricks.
  42. #                     You can also change a lot of parameters. See the
  43. #                   documentation of PSTricks for more customization.
  44. #
  45. # Input file format:  You can add blanks lines and comments (beginning by #).
  46. #                     The default field separator is | You can change it at
  47. #                   the AWK level.
  48. #                     The first non-comment line has 3 fields for titles
  49. #                   (only the first is required), and after you must have one
  50. #                   line by part of the pie chart, with 5 or 6 fields each
  51. #                   (percentage, inside label, outside label, filling, color,
  52. #                   explode). The last one is optional and must be "yes" if
  53. #                   you want that this part will be explode in the pie chart.
  54. #                     Obviously, the total of the percentages must be 100, or
  55. #                   less.
  56. #                     You can add (La)TeX commands in the labels (\em, \small,
  57. #                   etc.), and separate lines by \\ in LaTeX and \cr in TeX.
  58. #                   The text attributes cannot act on multiple lines : you
  59. #                   must type {\em Paris}\\{\em France} for example in LaTeX.
  60. #                     Be careful of some (La)TeX special characters (see $
  61. #                   and % in the examples).
  62. #                     Here are two examples, using a LaTeX syntax (suppress
  63. #                   the first 2 characters of each line):
  64. #
  65. # # Data for the first example of pie charts
  66. #
  67. # # Title of the figure      | Title for the table of figures | Label
  68. # Example 1 of pie chart
  69. #
  70. # # Value | Inside label  | Outside label                 | Type of    | Color
  71. # #                                                       |  filling   |  fill
  72. #      20 | \small\$9.0M  | {\em Paris}\\{\em France}     | hlines     | black
  73. #      35 | \small\$16.7M | {\em London}\\Great Britain   | vlines     | black
  74. #      45 | {\small\$23.1M}\\1991 | {\em Berlin}\\Germany | crosshatch | black
  75. #
  76. #
  77. # # Data for the second example of pie charts
  78. #
  79. # # Title of the figure      | Title for the table of figures | Label
  80. # \bf Example 2 of pie chart | Pie Chart 2                    | piechart2
  81. #
  82. # # Value | Inside | Outside label | Type of    | Color of | Explode part
  83. # #       |  label |               |  filling   |  filling |
  84. #       2 |        |  2\% \\ Last  | solid      | red      | yes
  85. #      28 |        | 28\%          | solid      | blue
  86. #       4 |        |  4\%          | solid      | green
  87. #      47 |        | 47\% \\ First | solid      | magenta  | yes
  88. #      19 |        | 19\%          | solid      | cyan
  89. #
  90. #
  91. # Examples        :   If you name these files piechart1.dat and piechart2.dat,
  92. #                   you can simply type:
  93. #                   piecharts.sh 1.5 <piechart1.dat >piechart1.tmp
  94. #                   piecharts.sh 0.7 figure boxit <piechart2.dat >pie2.tmp
  95. #                   and just insert the two result files in a LaTeX document
  96. #                   at the right place...
  97. #                     There are no difference if you want to produce plain TeX
  98. #                   code. You only must put valid plain TeX commands in the
  99. #                   data files.
  100. #
  101. # Portability     :   Expected great. Tested on RS6000 under AIX 3.2 and
  102. #                   DECStation 3100 under Ultrix 4.3
  103. #
  104. # Known problems  :   - Obviously, take care of the labels for the small
  105. #                   parts. If they are too long or in a too big font, the
  106. #                   result will not be pretty...
  107. #                     - You must have the LANG environment variable to En_US
  108. #                   if your UNIX has the National Language Support (NLS)
  109. #                   (by instance, amusing problems arrive with LANG=Fr_FR ...)
  110. #
  111. # Thanks          :   Timothy Van Zandt for the great work of PSTricks, tests
  112. #                   and ideas for the plain TeX mode.
  113. #
  114. # History         : (10/16/92) - rev. 1.0
  115. #                   (10/20/92) - rev. 1.1:
  116. #                      * increase the portability by suppression of the usage
  117. #                        of the sub builtin function of AWK
  118. #                      * better tracking of blank/null and comments lines
  119. #                   (12/04/92) - rev. 2.0:
  120. #                      * increase the portability by transformation of the
  121. #                        unique AWK program in a SHELL script calling an
  122. #                        AWK program :
  123. #                          - the program is renamed form piecharts.awk to
  124. #                            piecharts.sh
  125. #                          - input data file now given as parameter, not as
  126. #                            standard input
  127. #                          - the parameters are obtained in the SHELL script
  128. #                          - the options (SCALE, FIGURE and BOXIT) are now
  129. #                            treated by post-processing the AWK output file by
  130. #                            SED (ugly but it's because some AWK versions
  131. #                            doesn't accept the definition of external
  132. #                            variables...)
  133. #                      * change the unit for the values of the parts
  134. #                        from fractions of 360 degrees to percentages
  135. #                      * definition of the figure environment now optional,
  136. #                        using the FIGURE option
  137. #                      * new possibility to declare some explode parts in the
  138. #                        piechart
  139. #                      * use of the new \uput PSTricks macro for easy
  140. #                        treatment of explode parts
  141. #                      * possibility to have multiple (centered) line labels
  142. #                        (inside and outside labels)
  143. #                      * better tracking of syntax errors in the command line
  144. #                   (12/11/92) - rev. 2.1:
  145. #                      * generation of the tabular environment for labels only
  146. #                        if they have multi-lines
  147. #                      * the comment lines which begin with the ## are copied
  148. #                        (without the ##) at the beginning of the result file.
  149. #                        This allow automatic generation of input data file
  150. #                        with a good specification of the colors, by automatic
  151. #                        generation of the \newgray commands. It's to work in
  152. #                        conjonction with the piecharts-data.sh script.
  153. #                      * input data file read on the standard input
  154. #                      * diminution of the width of the minipage to avoid
  155. #                        the "overfull \hbox" problems
  156. #                      * change the test of the sum of percentages from 100 to
  157. #                        100.0001, because it can occurs problem with 100...
  158. #                   (01/26/93) - rev. 2.2:
  159. #                      * suppression of lines longer than 78 characters
  160. #                      * change the obsolete \Polar PSTricks order to
  161. #                        \SpecialCoor
  162. #                      * add the PLAIN option and the possibility to generate
  163. #                        plain TeX code
  164.  
  165.  
  166. # Interpretation of the parameters
  167. if [ $# -gt 3 ]
  168.    then echo "You cannot give more than 3 parameters."
  169.         echo "Syntax: piecharts.sh [scale factor] ([PLAIN] | [FIGURE] \
  170. [BOXIT]) <data_file  >out_file"
  171.         exit
  172. fi
  173.  
  174. SCALE=1
  175. FIGURE="xx"
  176. BOXIT="xx"
  177. TEX="LATEX"
  178. while [ $# -ne 0 ]
  179.   do if [ $1 = "BOXIT" ] || [ $1 = "boxit" ]
  180.         then BOXIT="BOXIT"
  181.         elif [ $1 = "FIGURE" ] || [ $1 = "figure" ]
  182.               then FIGURE="FIGURE"
  183.               elif [ $1 = "PLAIN" ] || [ $1 = "plain" ]
  184.                    then TEX="PLAIN"
  185.                    else SCALE=$1
  186.      fi
  187.      shift
  188. done
  189.  
  190. # Verifications for the plain TeX mode
  191. if [ $TEX = "PLAIN" ] && [ $BOXIT = "BOXIT" ]
  192.    then echo "You cannot give the BOXIT parameter in PLAIN TEX mode."
  193.         exit
  194. fi
  195. if [ $TEX = "PLAIN" ] && [ $FIGURE = "FIGURE" ]
  196.    then echo "You cannot give the FIGURE parameter in PLAIN TEX mode."
  197.         exit
  198. fi
  199.  
  200. # Beginning of the AWK program
  201. awk '
  202. BEGIN{FS="|"                              # Fields separator
  203.       PARAM="NO"
  204.       BEGIN_VALUE=0
  205.       END_VALUE=0
  206.       TOTAL_PERCENT=0
  207.       #   Definition of a figure environment (if FIGURE is not use, this line
  208.       # will be remove later - ugly but it is because some AWK versions does
  209.       # not accept the definition of external variables...)
  210.       # if FIGURE
  211.         print "%figure\\begin{figure}[htbp]"
  212.       #   Framebox around the figure, (if BOXIT is not use, this line will be
  213.       # remove later)
  214.       # if BOXIT
  215.         print "%boxit  \\psframebox[framesep=8mm]{"
  216.       print "%latex  {\\addtolength{\\textwidth}{-2.3cm}"
  217.       print "%latex   \\begin{minipage}{\\textwidth}"
  218.       print "%latex    \\begin{center}"
  219.       # if FIGURE
  220.         print "%figure     \\vspace{6mm}"
  221.       print "%latex     {\\setlength{\\tabcolsep}{0cm}"
  222.       print "%tex     {%"
  223.       print "      \\psset{unit=cm,xunit=cm,yunit=cm}"
  224.       print "%latex      \\begin{pspicture}(-2,-2)(2,2)"
  225.       print "        \\SpecialCoor"
  226.       print "%tex      \\pspicture(-2,-2)(2,2)"
  227.       print "%tex      \\def\\PieLabel\#1{\\vbox{\\halign{\\hfil\#\#"\
  228. "\\hfil\\cr\#1\\cr}}}%"
  229.       print "        \\psset{framesep=1.5pt}"
  230.       }
  231.  
  232. {
  233.  # Suppression of the right and left blanks of the variables
  234.  for (I=1;I<=NF+1;I++) {
  235.      # On the left...
  236.      for (J=1;J<=length($I)+1;J++)
  237.          if (substr($I,1,1) == " ")
  238.             $I=substr($I,2)
  239.           else
  240.             break
  241.      # On the right...
  242.      for (J=length($I);J>0;J--)
  243.          if (substr($I,J,1) == " ")
  244.             $I=substr($I,1,J-1)
  245.           else
  246.             break
  247.  }
  248.  
  249.  #   Special comments before title (beginning by ##): they are copied
  250.  # at the beginning of the result file, without the ##
  251.  if (substr($1,1,2) == "##")
  252.     print substr($1,3,length($1))
  253.  
  254.  # Comments, null and blank lines
  255.  if (substr($1,1,1) == "#" || $1 == "" || $1 == " ")
  256.     break
  257.  
  258.  if (PARAM == "NO") {
  259.      #   Generic parameters: title, title for the table of figures, label
  260.      # (the last two are optional)
  261.      PARAM="YES"
  262.      TITLE=$1
  263.      TABLE_TITLE=$2
  264.      LABEL=$3}
  265.    else {
  266.      # Verification of the sum of the parts
  267.      TOTAL_PERCENT=TOTAL_PERCENT+$1
  268.      if (TOTAL_PERCENT > 100.0001) {
  269.         print "Error! Total of percentages greater than 100! (" \
  270.               TOTAL_PERCENT ")"
  271.         exit}
  272.      # Transformation of percentages in fractions of 360 degrees
  273.      $1=$1*3.6
  274.      END_VALUE=END_VALUE+$1
  275.      VAL=BEGIN_VALUE+$1/2
  276.      #Explode part
  277.      if ($6 == "YES" || $6 == "yes")
  278.          print "       \\uput{0.3}[" VAL "](0;0){"
  279.      print "        \\pswedge[fillstyle=" $4 ",fillcolor=" $5 "]{2}{" \
  280.            BEGIN_VALUE "}{" END_VALUE "}"
  281.      # Inside label
  282.      if ($2 != "") {
  283.         print "%tex        \\rput(1.2;" VAL "){\\psframebox*{\\PieLabel{" \
  284. $2 "}}}"
  285.         if (index($2,"\\\\") == 0)
  286.            print "%latex        \\rput(1.2;" VAL "){\\psframebox*{" $2 "}}"
  287.          else {
  288.            print "%latex        \\rput(1.2;" VAL \
  289.                  "){\\psframebox*{\\begin{tabular}{c}"
  290.            print "%latex                                      " $2
  291.            print "%latex                                    \\end{tabular}}}"
  292.            }
  293.         }
  294.      # Outside label
  295.      if ($3 != "") {
  296.         print "%tex        \\uput{2.2}[" VAL "](0;0){\\PieLabel{" $3 "}}"
  297.         if (index($3,"\\\\") == 0)
  298.            print "%latex        \\uput{2.2}[" VAL "](0;0){" $3 "}"
  299.          else {
  300.            print "%latex        \\uput{2.2}[" VAL "](0;0){\\begin{tabular}{c}"
  301.            print "%latex                               " $3
  302.            print "%latex                             \\end{tabular}}"
  303.            }
  304.         }
  305.      # End of explode treatment
  306.      if ($6 == "YES" || $6 == "yes")
  307.         print "       }"
  308.      BEGIN_VALUE=END_VALUE}
  309.  $1=""
  310. }
  311.  
  312. END{
  313.     print "%latex      \\end{pspicture}"
  314.     print "%tex      \\endpspicture"
  315.     print "     }"
  316.     # For the case of a multiple line outside label at the bottom...
  317.     # if FIGURE
  318.       print "%figure     \\vspace{4mm}"
  319.     print "%latex    \\end{center}"
  320.     # if FIGURE
  321.       if (TABLE_TITLE == "")
  322.          print "%figure  \\caption{" TITLE "}"
  323.        else
  324.          print "%figure  \\caption[" TABLE_TITLE "]{" TITLE "}"
  325.       if (LABEL != "")
  326.          print "%figure  \\label{f:" LABEL "}"
  327.     print "%latex   \\end{minipage}"
  328.     print "%latex  }"
  329.     # if BOXIT
  330.       print "%boxit }"
  331.     # if FIGURE
  332.       print "%figure\\end{figure}"
  333.       }' >/tmp/piechart.tmp
  334.  
  335. #   Post-processing for the "scale factor", PLAIN, FIGURE and BOXIT parameters
  336. # (ugly but it's because some AWK versions doesn't accept the definition
  337. #  of external variables...)
  338. if [ $FIGURE = "FIGURE" ]
  339.    then sed -e /unit=/s/unit=/unit=$SCALE/g -e /\^%figure/s/// \
  340.             /tmp/piechart.tmp >/tmp/piechart.tmp2
  341.    else sed -e /unit=/s/unit=/unit=$SCALE/g -e /\^%figure/d \
  342.             /tmp/piechart.tmp >/tmp/piechart.tmp2
  343. fi
  344. mv /tmp/piechart.tmp2 /tmp/piechart.tmp
  345. if [ $BOXIT = "BOXIT" ]
  346.    then sed -e /\^%boxit/s/// /tmp/piechart.tmp >/tmp/piechart.tmp2
  347.    else sed -e /\^%boxit/d    /tmp/piechart.tmp >/tmp/piechart.tmp2
  348. fi
  349.  
  350. # plain TeX or LaTeX mode
  351. mv /tmp/piechart.tmp2 /tmp/piechart.tmp
  352. if [ $TEX = "LATEX" ]
  353.    then sed -e /\^%tex/d      /tmp/piechart.tmp >/tmp/piechart.tmp2
  354.         mv /tmp/piechart.tmp2 /tmp/piechart.tmp
  355.         sed -e /\^%latex/s/// /tmp/piechart.tmp >/tmp/piechart.tmp2
  356.    else sed -e /\^%latex/d    /tmp/piechart.tmp >/tmp/piechart.tmp2
  357.         mv /tmp/piechart.tmp2 /tmp/piechart.tmp
  358.         sed -e /\^%tex/s///   /tmp/piechart.tmp >/tmp/piechart.tmp2
  359. fi
  360.  
  361. # Deliver of the result!
  362. cat /tmp/piechart.tmp2
  363.  
  364. rm /tmp/piechart.tmp /tmp/piechart.tmp2
  365.